package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.model.base.BaseEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.core.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;
	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;
	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;
	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;
	@Autowired
	private BaseAttributeMapper baseAttributeMapper;
	@Autowired
	private BaseAttributeValueMapper baseAttributeValueMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {

		List<JSONObject> list = new ArrayList<>();

		//获取视图的中分类的所有数据
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);

		//将查询的数据根据category1_id进行分组，key：category1_id  value:List<BaseCategoryView>>
		Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		//用迭代器进行遍历
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();

		//遍历判断是否有下一个数据
		while (iterator.hasNext()) {

			//从迭代器中获取数据
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();

			Long category1Id = entry.getKey();

			List<BaseCategoryView> baseCategoryViewList1 = entry.getValue();

			//创建一级分类对象
			JSONObject category1 = new JSONObject();
			category1.put("categoryId",category1Id);
			category1.put("categoryName",baseCategoryViewList1.get(0).getCategory1Name());

			Map<Long, List<BaseCategoryView>> map2 = baseCategoryViewList1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator2 = map2.entrySet().iterator();

			//创建一个二级分类的集合
			List<JSONObject> category2List = new ArrayList<>();

			while (iterator2.hasNext()) {

				Map.Entry<Long, List<BaseCategoryView>> entry2 = iterator2.next();

				List<BaseCategoryView> baseCategoryViewList2 = entry2.getValue();

				Long category2Id = entry2.getKey();

				JSONObject category2 = new JSONObject();
				category2.put("categoryId",category2Id);
				category2.put("categoryName",baseCategoryViewList2.get(0).getCategory2Name());

				//声明一个集合来存储三级分类的数据
//				ArrayList<JSONObject> arrayList = new ArrayList<>();

				//获取三级分类集合放到二级分类中
//				baseCategoryViewList2.stream().forEach(baseCategoryView -> {
//
//					JSONObject category3 = new JSONObject();
//					category3.put("categoryId",baseCategoryView.getCategory3Id());
//					category3.put("categoryName",baseCategoryView.getCategory3Name());
//					arrayList.add(category3);
//				});

				List<JSONObject> arrayList = baseCategoryViewList2.stream().map(baseCategoryView -> {
					//	创建一个三级分类对象
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());

				//将三级分类的数据放到二级分类儿子集合中
				category2.put("categoryChild",arrayList);

				category2List.add(category2);
			}

			//二级分类集合添加到一级分类中
			category1.put("categoryChild",category2List);

			//将一级分类放入最外层集合中
			list.add(category1);

		}

		return list;
	}


	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {

		//根据一级分类id获取属性标签数据

		return baseAttributeMapper.selectAttribute(category1Id);

	}


	@Override
	@GuiGuCache(prefix = "category:")
	public BaseCategoryView getCategoryViewByCategory3Id(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {

		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));

		JSONObject category1 = new JSONObject();
		category1.put("categoryId", category1Id);
		category1.put("categoryName", baseCategoryViewList.get(0).getCategory1Name());

		//根据二级分类Id进行分组
		Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();

		ArrayList<JSONObject> categoryChildList = new ArrayList<>();

		//遍历二级分类的数据
		while (iterator.hasNext()) {
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();

			JSONObject category2 = new JSONObject();

			Long category2Id = entry.getKey();

			category2.put("categoryId",category2Id);
			category2.put("categoryName",entry.getValue().get(0).getCategory2Name());

			//获取三级分类数据
			List<JSONObject> categoryChild3List = entry.getValue().stream().map(item -> {
				JSONObject category3 = new JSONObject();

				category3.put("categoryId", item.getCategory3Id());
				category3.put("categoryName", item.getCategory3Name());

				return category3;
			}).collect(Collectors.toList());

			//添加三级数据
			category2.put("categoryChild",categoryChild3List);

			//添加二级数据
			categoryChildList.add(category2);
		}

		//往一级分类种添加二级分类数据
		category1.put("categoryChild",categoryChildList);

		return category1;
	}


	@Override
	public List<BaseCategory3> findTopBaseCategory3ByCategory1Id(Long category1Id) {

		//通过一级分类id找到对应二级分类对象
		List<BaseCategory2> baseCategory2List = this.baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));

		//拿到二级分类id集合
		List<Long> category2IdList = baseCategory2List.stream().map(BaseEntity::getId).collect(Collectors.toList());

		List<BaseCategory3> baseCategory3List = this.baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
				.in(BaseCategory3::getCategory2Id, category2IdList)
				.eq(BaseCategory3::getIsTop, 1)
				.last("limit 7"));

		return baseCategory3List;
	}


	@Override
	public List<BaseCategory1> findAllCategory1() {
		return baseCategory1Mapper.selectList(null);
	}
}
